package com.redisdemo.demo.stream;

import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisStreamCommands;
import redis.clients.jedis.Jedis;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {

    public static void main(String[] args) {
        RedisURI redisUri = RedisURI.Builder.redis("127.0.0.1")
                .withPort(6379)
               .withPassword("")
                .withDatabase(0)
                .build();
//        RedisURI redisUri = RedisURI.create("127.0.0.1", 6379);
        RedisClient redisClient = RedisClient.create(redisUri);
        StatefulRedisConnection<String, String> connection = redisClient.connect();
        RedisStreamCommands<String, String> streamCommands = connection.sync();
        System.out.println("长度：" + streamCommands.xlen("first-stream"));
        //获取分组信息
        List<Object> ll = streamCommands.xinfoGroups("first-stream");
        if (ll != null && ll.size() > 0) {
            for (Object o : ll) {
                List<Object> a= (List<Object>) o;
                for (Object s : a) {
                    System.out.println(s.toString());
                }
            }
        }else{
            streamCommands.xgroupCreate(XReadArgs.StreamOffset.latest("first-stream"), "lettuce-group");
        }
//      //强制删除 不管消费不消费
        //MAXLEN选项和实际计数之间的~参数表示用户并不是真正在要求流长度恰好是1000个项目，
        // 而是可以增加几十个条目，但不能少于1000个项目. 使用此选项修饰符时，
        // 仅当Redis能够删除整个宏节点时才执行修剪. 这使其效率更高，通常就是您想要的.
        streamCommands.xtrim("first-stream",false,10);
       System.out.println("长度：" + streamCommands.xlen("first-stream"));
        // streamCommands.xtrim("first-stream",2000);
        Map<String, String> body;

//        int a=50;
//        for (int n = 0; n < a; n++) {
//            body =  new HashMap<>(16);
//            body.put("-"+n,"我是第"+n);
//            String messageId = streamCommands.xadd("first-stream", body);
//            System.out.println("长度："+streamCommands.xlen("first-stream"));
//            System.out.println(messageId);
//        }
//        List<Object> lll=streamCommands.xinfoStream("first-stream");
//        List<Object> ll=streamCommands.xinfoGroups("lettuce-group");
//        for (Object o : ll) {
//            System.out.println(o.toString());
//        }

//       // streamCommands.xgroupCreate(XReadArgs.StreamOffset.latest("first-stream"), "lettuce-group");
//        List<StreamMessage<String, String>> messages = streamCommands.xreadgroup(
//                Consumer.from("lettuce-group", "lg1"),
//                XReadArgs.Builder.block(10000),
//                XReadArgs.StreamOffset.lastConsumed("first-stream"));
//
//        if(messages.size() == 1) {
//            System.out.println(messages.get(0).getId()+"------");
//            System.out.println(messages.get(0).getBody()+"+++++++");
//        } else { // no message was read
//
//        }
        connection.close();
        redisClient.shutdown();
    }

}
